package com.easybuilder.game.dto.cs;

import com.easybuilder.game.dto.base.Goods;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Container { // 容器：存放物品

    private Long id;
    /** 背包名称 */
    private String name;
    /** 最大空间 */
    private Double maxSpace;
    /** 已使用空间 */
    private Double usedSpace = 0D;
    /** 剩余空间 */
    private Double leftSpace = maxSpace;
    /** 存放物品集合(物品列表) */
    private final LinkedList<Goods> GOODS_LIST = new LinkedList<>();
    /** 物品数量（key:物品id;value:物品数量） */
    private final Map<Long, Integer> GOODS_NUM_MAP = new HashMap<>();
    /** 操作背包锁对象 */
    private final Object LOCK = new Object();


    /*初始化*/
    public Container(Long id, String name, Double maxSpace) {
        this.id = id;
        this.name = name;
        this.maxSpace = maxSpace;
        this.usedSpace = 0D;
        this.leftSpace = maxSpace;
    }

    /** 添加物品
     * @param goods 物品对象
     * @param num 物品数量
     * @return 返回添加成功的物品数量
     * */
    public Integer add(Goods goods, Integer num){
        synchronized (LOCK) {
            // 计算可存放物品的最大数量
            int addAbleNum = (int)(leftSpace / goods.getSpace());
            Integer existNum = GOODS_NUM_MAP.get(goods.getId());
            Integer total = existNum == null ? addAbleNum : existNum+addAbleNum;
            GOODS_NUM_MAP.put(goods.getId(), total);
            // 物品添加到背包列表
            if (!GOODS_LIST.contains(goods)) {
                GOODS_LIST.addLast(goods);
            }
            return addAbleNum;
        }
    }


    /** 移除物品
     * @param goods 物品对象
     * @param num 物品数量
     * @return 返回添加成功的物品数量
     * */
    public void remove(Goods goods, Integer num){
        synchronized (LOCK) {

        }
    }


    public static void main(String[] args) {

    }
}
